Write a program in C++ to demonstrate the manipulators

Assignment 12

Write a program in C++ to demonstrate the manipulators.

Program Code with explanation:

1.    #include <iostream>

2.    #include <iomanip>

3.    using namespace std;

4.    int main( )

5.    {

6.                int x1=111,x2= 222, x3=333;

7.                cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

8.                cout << "\t \t Demo of setw with value 10 \n";

9.                cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

10.             cout << setw(10) << "TestDemo" << setw(20) << "Values" << endl;

11.             cout << setw(10) << "Testing" << setw(20)<< x1 << endl;

12.             cout << setw(10) << "Result" << setw(20)<< x2 << endl;

13.             cout << setw(10) << "OnOff" << setw(20)<< x3 << endl;

14.             cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

15.             cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

16.             cout << "\t \t Demo of setfill with value '*' using setw(15) \n";

17.             cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

18.             cout << setw(15) << setfill('*') << 6789 << " OK" << endl;

19.             cout << setw(15) << setfill('*') << 789 << " OK" << endl;

20.             cout << setw(15) << setfill('*') << 89 << " OK" << endl;

21.             cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

22.             cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

23.             cout << "\t \t Demo of fixed and scientific value \n";

24.             cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

25.             float x = 0.12345;

26.             cout << " fixed " << setprecision(5) << x << endl;

27.             cout << " fixed " << setprecision(4) << x << endl;

28.             cout << " fixed " << setprecision(3) << x << endl;

29.             cout << " scientific " << x << endl;

30.             return 0;

31. }

Explanation of Code:

Line No. 1, 2 and 3: include the Library file and stander input – output functions.

Line No. 4: main function header.

Line No. 5 and 31: Begin and end of main function respectively.

Line No. 6: Declare the integer variables.

Line No. 10 to 13: Demo of setw manipulators.

setw manipulators means set width.

<iomanip> header file contain the setw manipulators.

It is used to display specific number of characters filling with space before it, if numbers of characters are not enough.     

Line No. 18 to 20: Demo of setfill manipulators.

setfill manipulators means set fill character.

<iomanip> header file contain the setfill manipulators.

It is used to display specific number of characters filling with specific   character before it, if numbers of characters are not enough.

Line No. 26 to 29: Demo of fixed and scientific value manipulators.

            setprecision is used to display the specific number of precision of float number.

            Line No. 30: return statement appropriate with main function return data type.

Program Code for run:

#include <iostream>

#include <iomanip>

using namespace std;

int main( )

{

int x1=111, x2= 222, x3=333;

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

cout << "\t \t Demo of setw with value 10 \n";

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

cout << setw(10) << "TestDemo" << setw(20) << "Values" << endl;

cout << setw(10) << "Testing" << setw(20)<< x1 << endl;

cout << setw(10) << "Result" << setw(20)<< x2 << endl;

cout << setw(10) << "OnOff" << setw(20)<< x3 << endl;

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

 

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

cout << "\t \t Demo of setfill with value '*' using setw(15) \n";

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

cout << setw(15) << setfill('*') << 6789 << " OK" << endl;

cout << setw(15) << setfill('*') << 789 << " OK" << endl;

cout << setw(15) << setfill('*') << 89 << " OK" << endl;

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

 

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

cout << "\t \t Demo of fixed and scientific value \n";

cout << " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n";

float x = 0.12345;

cout << " fixed " << setprecision(5) << x << endl;

cout << " fixed " << setprecision(4) << x << endl;

cout << " fixed " << setprecision(3) << x << endl;

cout << " scientific " << scientific << x << endl;

 

return 0;

}

Output of Program:

            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                         Demo of setw with value 10
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                        TestDemo              Values
                             Testing                 111
                              Result                 222
                              OnOff                 333
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                        Demo of setfill with value '*' using setw(15)
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                        ***********6789 OK
                        ************789 OK
                        *************89 OK
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                        Demo of fixed and scientific value
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                        fixed 0.12345
                        fixed 0.1235
                        fixed 0.123
                        scientific 1.235e-01

 

Comments